/*********************************************************************
 * Módulo 3. Tutorial de desarrollo con NME. Bucle de juego
 * Programación Multimedia y Juegos
 * Autor: David Vallejo Fernández    david.vallejo@tegnix.com
 * Autor: Carlos González Morcillo   carlos.gonzalez@tegnix.com
 *
 * You can redistribute and/or modify this file under the terms of the
 * GNU General Public License ad published by the Free Software
 * Foundation, either version 3 of the License, or (at your option)
 * and later version. See <http://www.gnu.org/licenses/>.
 *
 * This file is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.  
 *********************************************************************/

package;

import com.eclecticdesignstudio.motion.Actuate;
import com.eclecticdesignstudio.motion.easing.Quad;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageQuality;
import flash.display.StageScaleMode;
import flash.Lib;

class Circle extends Sprite {
  
  private var _size  : Float;
  private var _blur  : Float;
  private var _alpha : Float;
  private var color : Int;
  
  public function new () {
    super ();
    
    // Generación aleatoria de info básica del círculo.
    _size = 5 + Math.random () * 35 + 20;
    _blur = 3 + Math.random () * 12;
    _alpha = 0.2 + Math.random () * 0.6;
    
    // Heredadas de Sprite.
    this.color = Std.int (Math.random () * 0xFFFFFF);
    this.graphics.beginFill (color);
    this.graphics.drawCircle (0, 0, _size);
    this.x = Math.random () * Lib.current.stage.stageWidth;
    this.y = Math.random () * Lib.current.stage.stageHeight;    
  }
  
  public function animate () : Void {
    var duration:Float = 1.5 + Math.random () * 4.5;
    var targetX:Float = Math.random () * Lib.current.stage.stageWidth;
    var targetY:Float = Math.random () * Lib.current.stage.stageHeight;
    
    // La animación se delega en la biblioteca Actuate.
    Actuate.tween (this, duration, { x: targetX, y: targetY }, false)
      .ease (Quad.easeOut).onComplete (animate, [ this ]);
  }

  public function pause () : Void {
    Actuate.pause(this);
  }

  public function resume () : Void {
    Actuate.resume(this);
  }

  // Oscurece el color del círculo.
  public function fillInBlack () : Void {
    this.graphics.beginFill (Std.int (0x000000));
    this.graphics.drawCircle (0, 0, _size);
  }
  
  public function fillInOriginal () : Void {
    this.graphics.beginFill (this.color);
    this.graphics.drawCircle (0, 0, _size);
  }

  // ¿Impacto sobre el círculo en el espacio 2D?
  public function impact (x:Float, y:Float) : Bool {
    if ((x < this.x + this._size) && (x > this.x - this._size)) {
      if ((y < this.y + this._size) && (y > this.y - this._size)) {
	return true;
      }
    }
    return false;
  }
  
  public function getColor() : Int {
        return this.color;
  }
  
  public function getSize() : Float {
        return this._size;
  }
}
